// @author: caoshi
// @date: 2021-05-21 19:00
package util

import (
	"bytes"
	"math/rand"
)

const (
	numberLetterStr = "0123456789abcdefghijklmnopqrstuvwxyz"
	length          = 32 // 生成UUID的长度
)

func GetUUID() string {
	var res = ""
	for i := 0; i < length; i++ {
		index := rand.Intn(36) // [0, 36) 之间的随机整数
		s := Index(numberLetterStr, uint(index))
		res += s
	}

	return res
}

func GetRandomString(l int, r *rand.Rand) string {
	str := "0123456789abcdefghijklmnopqrstuvwxyz"
	byts := []byte(str)
	var result []byte
	for i := 0; i < l; i++ {
		result = append(result, byts[r.Intn(len(byts))])
	}
	return string(result) // 转成string
}

// 获取字符串指定位置上的字符
func Index(s string, index uint) string {
	runes := bytes.Runes([]byte(s))
	for i, r := range runes {
		if i == int(index) {
			return string(r)
		}
	}
	return ""
}
